package tech.gismap.admin.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tech.gismap.admin.mapper.OaRoleMapper;
import tech.gismap.admin.service.OaRoleService;
import tech.gismap.admin.domain.OaRole;
import tech.gismap.admin.web.bo.OaRoleBo;
import tech.gismap.admin.web.vo.OaRoleVo;
import tech.gismap.common.core.domain.R;
import tech.gismap.common.core.utils.MapstructUtils;
import tech.gismap.common.log.annotation.Log;
import tech.gismap.common.log.enums.BusinessType;
import tech.gismap.common.mybatis.core.page.PageQuery;
import tech.gismap.common.mybatis.core.page.TableDataInfo;
import tech.gismap.common.web.core.BaseController;

import java.util.Arrays;

/**
 * 角色
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/oa/role")
public class OaRoleController extends BaseController {

    private final OaRoleMapper baseMapper;

    private final OaRoleService baseService;

    /**
     * 删除角色
     *
     * @param ids 标识码
     */
    @DeleteMapping("/{ids}")
    @SaCheckPermission("oa:role:remove")
    @Log(title = "角色", businessType = BusinessType.DELETE)
    public R<Void> deleteByIds(@PathVariable Long[] ids) {
        return toAjax(baseMapper.deleteByIds(Arrays.asList(ids)));
    }

    /**
     * 新增角色
     */
    @PostMapping
    @SaCheckPermission("oa:role:add")
    @Log(title = "角色", businessType = BusinessType.INSERT)
    public R<Void> insert(@Validated @RequestBody OaRoleBo bo) {
        return toAjax(baseMapper.insert(MapstructUtils.convert(bo, OaRole.class)));
    }

    /**
     * 修改角色
     */
    @PutMapping
    @SaCheckPermission("oa:role:edit")
    @Log(title = "角色", businessType = BusinessType.UPDATE)
    public R<Void> updateById(@Validated @RequestBody OaRoleBo bo) {
        return toAjax(baseMapper.updateById(MapstructUtils.convert(bo, OaRole.class)));
    }

    /**
     * 根据角色标识码获取详细信息
     *
     * @param id 标识码
     */
    @GetMapping(value = "/{id}")
    @SaCheckPermission("oa:role:query")
    @Log(title = "角色", businessType = BusinessType.LIST)
    public R<OaRoleVo> selectVoById(@PathVariable Long id) {
        return R.ok(baseMapper.selectVoById(id));
    }

    /**
     * 获取角色信息列表
     */
    @GetMapping("/list")
    @SaCheckPermission("oa:role:list")
    @Log(title = "角色", businessType = BusinessType.SELECT)
    public TableDataInfo<OaRoleVo> selectPageList(OaRoleBo bo, PageQuery pageQuery) {
        return baseService.selectPageList(bo,pageQuery);
    }
}
